package com.biglead.mini.controller;


import com.biglead.common.pojo.CommonResult;
import com.biglead.common.pojo.OrderInfo;
import com.biglead.feign.clients.FeignOrderClient;
import com.biglead.mini.config.SnowFlakeCompone;
import com.biglead.mini.service.OrderService;
import com.biglead.mini.service.RedisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单管理Controller
 */
@Controller
@Api(tags = "OmsPortalOrderController")

@RequestMapping("/order")
public class OrderController {
    @Autowired
    private FeignOrderClient feignOrderClient;

    @Autowired
    private OrderService orderService;

    @ApiOperation("立即购买下单-乐观锁")
    @RequestMapping(value = "/confirmOrder", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<OrderInfo> confirmOrder(@RequestHeader Long userId, @RequestParam Long goodsId) {
        OrderInfo confirmOrderResult = feignOrderClient.createOrder(userId, goodsId);
        return CommonResult.success(confirmOrderResult);
    }

    @ApiOperation("立即购买下单-乐观锁、Redis缓存")
    @RequestMapping(value = "/createWithRedis", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<OrderInfo> createWithRedis(@RequestHeader Long userId, @RequestParam Long goodsId) {
        OrderInfo confirmOrderResult = feignOrderClient.createWithRedis(userId, goodsId);
        return CommonResult.success(confirmOrderResult);
    }


    @ApiOperation("立即购买下单-异步 kafka ")
    @RequestMapping(value = "/createWithKafka", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<String> createWithKafka(@RequestHeader Long userId, @RequestParam Long goodsId) {

        String sn = orderService.asyncCreatOrder(userId, goodsId);

        return CommonResult.success(sn);
    }

    @ApiOperation("查询订单详情-前端轮循环")
    @RequestMapping(value = "/info/sn", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<OrderInfo> getOrderDetailFromSn(@RequestHeader Long userId, @RequestParam String sn) {

        OrderInfo orderInfo = orderService.getOrderInfoBySn(sn);

        return CommonResult.success(orderInfo);
    }
}
